import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import undetected_chromedriver as uc

options = webdriver.ChromeOptions()
options.add_argument(
    'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36')

driver = uc.Chrome(options=options)

driver.get("https://sf-item.taobao.com/sf_item/796202490346.htm")

time.sleep(5)

try:
    slider = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, '.nc_iconfont.btn_slide'))
    )

    print("滑块找到")

    action = ActionChains(driver)

    slider_track = driver.find_element(By.CLASS_NAME, 'nc_bg')
    slider_width = slider_track.size['width']
    print(slider_width)

    action.click_and_hold(slider).perform()
    time.sleep(1)

    target_distance = 300
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)

    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)

    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(10)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(30)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)
    action.move_by_offset(xoffset=10, yoffset=0).perform()
    time.sleep(0.3)

    action.release().perform()

    print("滑动完成")

except Exception as e:
    print(f"滑块滑动失败: {e}")

finally:
    time.sleep(500)
    driver.quit()
